package com.db.collections.listinsert;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class ListsInsertTest {
    public static final int cycles = 00_000;

    public static void main(String[] args) {
        List<String> linkedList = new LinkedList<>();
        List<String> arrayList = new ArrayList<>(100000);
        Random rand = new Random();
        int counts = 0;

        for (int i = 0; i < cycles; i++) {
            linkedList.add(0, "STR" + rand.nextInt());
            arrayList.add(0, "STR" + rand.nextInt());
        }
        linkedList.clear();
        arrayList.clear();

        long start, finish, linkedTime, arrayTime;

        linkedTime = arrayTime = 0;
        while (counts < 100 || arrayTime <= linkedTime) {
            start = System.nanoTime();
            linkedList.add(0, "TEST" + rand.nextInt());
            linkedList.add(0, "TEST" + rand.nextInt());
            linkedList.add(0, "TEST" + rand.nextInt());
            linkedList.add(0, "TEST" + rand.nextInt());
            linkedList.add(0, "TEST" + rand.nextInt());
            linkedList.add(0, "TEST" + rand.nextInt());
            linkedList.add(0, "TEST" + rand.nextInt());
            linkedList.add(0, "TEST" + rand.nextInt());
            linkedList.add(0, "TEST" + rand.nextInt());
            linkedList.add(0, "TEST" + rand.nextInt());
            finish = System.nanoTime();
            linkedTime = finish - start;
            linkedTime = (long) (linkedTime / 10.);

            start = System.nanoTime();
            arrayList.add(0, "TEST" + rand.nextInt());
            arrayList.add(0, "TEST" + rand.nextInt());
            arrayList.add(0, "TEST" + rand.nextInt());
            arrayList.add(0, "TEST" + rand.nextInt());
            arrayList.add(0, "TEST" + rand.nextInt());
            arrayList.add(0, "TEST" + rand.nextInt());
            arrayList.add(0, "TEST" + rand.nextInt());
            arrayList.add(0, "TEST" + rand.nextInt());
            arrayList.add(0, "TEST" + rand.nextInt());
            arrayList.add(0, "TEST" + rand.nextInt());
            finish = System.nanoTime();
            arrayTime = finish - start;
            arrayTime = (long) (arrayTime / 10.);

            if (arrayTime > linkedTime) {
                System.out.println("size " + arrayList.size());
                System.out.println("ArrayList time " + arrayTime);
                System.out.println("LinkedList time " + linkedTime);
                counts++;
            }
        }

    }
}
/*
...
size 1580
ArrayList time 4725
LinkedList time 3903
* */